package cn.flying.cloud.system.position.service.impl;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.system.position.entity.SysPostUser;
import cn.flying.cloud.system.position.mapper.SysPostUserMapper;
import cn.flying.cloud.system.position.service.SysPostUserService;

/**
 * <p>
 * 用户岗位信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-02 13:07:01
 */
@Service
public class SysPostUserServiceImpl extends ServiceImpl<SysPostUserMapper, SysPostUser> implements SysPostUserService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 分页查询用户岗位信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysPostUser> getPageList(PageRequest<SysPostUser> request) {
        SysPostUser param = request.getParam();
        LambdaQueryWrapper<SysPostUser> wrapper = getLambdaQueryWrapper(param);
        Page<SysPostUser> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysPostUser> getLambdaQueryWrapper(SysPostUser param) {
        LambdaQueryWrapper<SysPostUser> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getPostId())) {
                wrapper.eq(SysPostUser::getPostId, param.getPostId());
            }
            if (Ft.String.isNotBlank(param.getUserId())) {
                wrapper.eq(SysPostUser::getUserId, param.getUserId());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询用户岗位信息表数据
     *
     * @param sysPostUser
     * @return
     */
    @Override
    public List<SysPostUser> getList(SysPostUser sysPostUser) {
        LambdaQueryWrapper<SysPostUser> wrapper = getLambdaQueryWrapper(sysPostUser);
        List<SysPostUser> list = this.list(wrapper);
        return list;
    }

    /**
     * 查询用户岗位信息表详情数据
     *
     * @param sysPostUser
     * @return
     */
    @Override
    public SysPostUser getOneObj(SysPostUser sysPostUser) {
        LambdaQueryWrapper<SysPostUser> wrapper = getLambdaQueryWrapper(sysPostUser);
        return this.getOne(wrapper);
    }


    /**
     * 新增用户岗位信息表数据
     *
     * @param sysPostUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysPostUser sysPostUser) {
        return this.save(sysPostUser);
    }

    /**
     * 更新用户岗位信息表数据
     *
     * @param sysPostUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysPostUser sysPostUser) {
        return this.saveOrUpdate(sysPostUser);
    }

    /**
     * 删除用户岗位信息表数据
     *
     * @param sysPostUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysPostUser sysPostUser) {
        LambdaQueryWrapper<SysPostUser> wrapper = new LambdaQueryWrapper<>();
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
